package com.jy.rhin.support.notification;

import com.jy.rhin.support.notification.convertor.BaseNotificationConverter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description: 消息转换器工厂
 * @Author: lww
 * @Date: 2022/7/7 15:42
 */
@Component
public class NotificationConvertFactory {

    @Resource
    Map<String, BaseNotificationConverter> map;

    /**
     * 默认消息转换器
     */
    private static final String DEFAULT_CONVERTER_NAME = "defaultConverter";

    private static final String COLON = ":";

    private static final String BEAN_NAME_SUF = "Converter";

    /**
     * 获取对应的消息转换器
     *
     * @param topicExp
     * @return
     */
    public BaseNotificationConverter getConvert(String topicExp) {
        String converterName = topicExp.substring(topicExp.indexOf(COLON) + 1) + BEAN_NAME_SUF;
        if (!map.containsKey(converterName)) {
            return map.get(DEFAULT_CONVERTER_NAME);
        }
        return map.get(converterName);
    }

}
